package com.chixing.controller;


import com.chixing.pojo.Orders;
import com.chixing.service.OrderService;
import com.chixing.util.RandomUtil;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;


@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("orders/save")
    public ServerResponse save(@RequestParam("sumPrice") Float sumPrice){
        Orders orders =new Orders();
        
      /*  orders.setOrderId(Integer.valueOf(RandomUtil.createRandNum1()));*/
        orders.setCustId(3);
        orders.setHotelId(1001);
        orders.setOrderPrice(sumPrice);
        orders.setOrderState(1);
        orders.setOrderTime(new Date());
        return orderService.save(orders);
    }
    @RequestMapping("orders/all")
    public ModelAndView getAll(){
        List<Orders> ordersList = orderService.getAll();
        Orders orders = ordersList.get(ordersList.size()-1);
        Float price = orders.getOrderPrice();
        Integer id = orders.getOrderId();

        ModelAndView mav = new ModelAndView();
        mav.addObject("price",price);
        mav.addObject("id",id);

        mav.setViewName("customer/pay");
        return mav;
    }
    @RequestMapping("orders/{id}")
    public Orders getById(@PathVariable("id") Integer id){
        return orderService.getById(id);
    }


}
